<?php

$order = $_SESSION['paging'][$path_string]['order'];
$filter = isset($_SESSION['paging'][$path_string]['filter']) ? '%' . $_SESSION['paging'][$path_string]['filter'] . '%' : '%';

// The default is to order by id when no other order is set, but for the orders we
// want id DESC so override this here.
if (isset($_REQUEST['paging']['order'])) $_SESSION['paging'][$path_string]['order_set'] = 1;
if (!isset($_SESSION['paging'][$path_string]['order_set'])) {
	$order = 'guid';
}

$query = $db->select()
            ->from(array('l' => 'cms_email_templates'))
            ->order($order)
            ->where($db->quoteInto('site_id=?', $site['id']))
            ->where($db->quoteInto('guid LIKE ? OR description LIKE ? OR default_subject LIKE ? OR default_from LIKE ? OR default_to LIKE ?', $filter));
$email_templates = $db->fetchAll($query);

$page   = $_SESSION['paging'][$path_string]['page'];
$show   = $_SESSION['paging'][$path_string]['show'];
$offset = ($page - 1) * $show;
$total  = count($email_templates);

$smarty->assign('order', $order);
$smarty->assign('page', $page);
$smarty->assign('show', $show);
$smarty->assign('filter', $filter);
$smarty->assign('offset', $offset);
$smarty->assign('total', $total);

$smarty->assign('email_templates', array_slice($email_templates, $offset, $show));

if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) and $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') {
	header('Content-type: text/xml');
	$smarty->display('global/email-templates/index.tpl.taconite');
} else {
	$smarty->display('global/email-templates/index.tpl.html');
}